#include "Mainwindow.h"
#include <qdialog.h>
#include "qstring.h"
#include "qtextedit.h"
#include "User.h"
#include "Takes.h"
#include "UserDB.h"
#include "TakesDB.h"
#include "CourseDB.h"
#include "TeacherDB.h"
#include <QMessageBox>
#include <QTextCodec>  //添加头文
#include "InfoManageService.h"
#include "CourseManageService.h"
#include "ScoreManagement.h"

Mainwindow::Mainwindow(QWidget *parent)
	: QMainWindow(parent)
{
	QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); //设置编码
	ui.setupUi(this);
}

Mainwindow::Mainwindow(LoginDialog *login,QWidget *parent)
	: QMainWindow(parent)
{
	loginDialog = login;
	ui.setupUi(this);
}


void Mainwindow::on_stuMenuTree_itemClicked(QTreeWidgetItem *item, int column)
{
	CourseManageService courseService;
	QString s = item->text(column);
	if (s== "修改密码")
	{
		ui.stuSearchScoreView->hide();
		ui.selectCourseView->hide();
		ui.userInfiView->hide();
		ui.couseSelectedView->hide();
		ui.updaPasswordView->show();

	}
	else if(s=="个人信息"){
		ui.stuSearchScoreView->hide();
		ui.selectCourseView->hide();
		ui.updaPasswordView->hide();
		ui.couseSelectedView->hide();
		ui.userInfiView->show();

		InfoManageService infoService;
		string id = this->getLoginUserId().trimmed().toStdString();
		Student stu = infoService.lookStudentInfo(id);
		ui.idEdit->setText(QString::fromStdString(stu.getId()));
		ui.nameEdit->setText(QString::fromStdString(stu.getName()));
		ui.sexEdit->setText(QString::fromStdString(stu.getSex()));
		ui.classEdit->setText(QString::fromStdString(stu.getClassName()));
		ui.deptNameEdit->setText(QString::fromStdString(stu.getDeptName()));
		
	}
	else if (s.contains("已选课")) {
		ui.stuSearchScoreView->hide();
		ui.selectCourseView->hide();
		ui.updaPasswordView->hide();
		ui.userInfiView->hide();
		ui.couseSelectedView->show();
	
		string id = this->getLoginUserId().trimmed().toStdString();
		list<Course> list = courseService.lookStuHasCourses(id);
		QTableWidget *table = ui.courseTable;
		table->setRowCount(list.size());
		table->setColumnWidth(0, 300);//课程id
		table->setColumnWidth(1, 450);//课程名
		table->setColumnWidth(2, 200);//教师
		table->setColumnWidth(3, 350);//开课学院
		int i = 0;
		for each(Course c in list) {
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
			TeacherDB tDB;
			Teacher t = tDB.selectById(c.getTeacherId());
			table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(t.getName())));
			table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(t.getDeptName())));
			i++;
		}
	}
	else if (s.contains("退选课")) {
		ui.stuSearchScoreView->hide();
		allCourseTableFlag = 0;
		ui.couseSelectedView->hide();
		ui.updaPasswordView->hide();
		ui.userInfiView->hide();
		ui.selectCourseView->show();
		CourseDB cDB;
		string id = this->getLoginUserId().trimmed().toStdString();
		list<Course> selectedCourseList = courseService.lookStuHasCourses(id);

		list<Course> courseList = cDB.selectAll();
		QTableWidget *table = ui.allCourseTable;
		table->setRowCount(courseList.size());
		table->setColumnWidth(0, 200);//课程id
		table->setColumnWidth(1, 400);//课程名
		table->setColumnWidth(2, 200);//教师
		table->setColumnWidth(3, 300);//开课学院
		table->setColumnWidth(4, 200);//是否选课
		int i = 0;
		for each(Course c in courseList) {
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));
			TeacherDB tDB;
			Teacher t = tDB.selectById(c.getTeacherId());
			table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(t.getName())));
			table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(t.getDeptName())));
			QTableWidgetItem *check = new QTableWidgetItem();
			bool flag = false;
			for each(Course co in selectedCourseList)
			{
				if (co.getCourseId() == c.getCourseId()) {
					flag = true;
				}
			}
			if (flag) {
				check->setCheckState(Qt::Checked);
			}
			else {
				check->setCheckState(Qt::Unchecked);
			}
			table->setItem(i, 4, check);
			i++;
		}
		allCourseTableFlag = 1;
	}
	else if (s == "查看成绩") {
		ui.selectCourseView->hide();
		ui.userInfiView->hide();
		ui.couseSelectedView->hide();
		ui.updaPasswordView->hide();
		ui.stuSearchScoreView->show();

		string id = this->getLoginUserId().trimmed().toStdString();
		list<Course> list = courseService.lookStuHasCourses(id);
		QTableWidget *table = ui.scoreTable;
		table->setRowCount(list.size());
		table->setColumnWidth(0, 260);//课程id
		table->setColumnWidth(1, 460);//课程名
		table->setColumnWidth(2, 140);//平时成绩
		table->setColumnWidth(3, 140);//期末成绩
		table->setColumnWidth(4, 140);//总评成绩
		table->setColumnWidth(5, 140);//是否通过
		
		ScoreManagement scoreService;
		int i = 0;
		for each(Course c in list) {
			table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(c.getCourseId())));//第一行第一列的内容设置
			table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(c.getTitle())));

			Grade g = scoreService.StuCheckOneScore(id, c.getCourseId());
			if (g.getGradeId() != "")
			{
				table->setItem(i, 2, new QTableWidgetItem(QString::number(g.getOrdinary(), 10, 2)));
				table->setItem(i, 3, new QTableWidgetItem(QString::number(g.getGfinal(), 10, 2)));
				table->setItem(i, 4, new QTableWidgetItem(QString::number(g.getTotal(), 10, 2)));
				if (g.getTotal() >= 60) {
					table->setItem(i, 5, new QTableWidgetItem("YES"));
				}
				else {
					table->setItem(i, 5, new QTableWidgetItem("NO"));
				}
			}
		
			
			i++;
		}
	}
}

void Mainwindow::on_changepwBtn_clicked() 
{//修改密码
	string id = this->getLoginUserId().trimmed().toStdString();
	
	string oldpw = ui.oldpwEdit->text().trimmed().toStdString();
	string newpw = ui.newpwEdit->text().trimmed().toStdString();
	string newrpw = ui.newpwrEdit->text().trimmed().toStdString();
	UserDB uDB;
	User u = uDB.selectByUserId(id);
	if(oldpw==""||newpw==""||newrpw=="") {
		QMessageBox::warning(this, tr("Waring"), tr("输入不能为空!"), QMessageBox::Yes);
	}
	else if (newpw!=newrpw) 
	{
		QMessageBox::warning(this, tr("Waring"),tr("两次输入的密码不正确!"), QMessageBox::Yes);
	}
	else if(u.getPassword()!=oldpw){
		QMessageBox::warning(this, tr("Waring"),tr("旧密码不正确!"), QMessageBox::Yes);
	}
	else if(u.getPassword()==oldpw&&newpw==newrpw){
		u.setPassword(newpw);
		uDB.update(u);
		QMessageBox::warning(this, tr("Waring"), tr("修改密码成功!"), QMessageBox::Yes);
		ui.newpwEdit->clear();
		ui.newpwrEdit->clear();
		ui.oldpwEdit->clear();
	}
	
}

Ui::MainwindowClass Mainwindow::getUi()
{
	return ui;
}

QString Mainwindow::getLoginUserId()
{
	QString id = ui.loginUserLabel->text();
	return id;
}

void Mainwindow::on_allCourseTable_cellChanged(int row, int col)
{//退选课程操作
	if (allCourseTableFlag == 1) {
		CourseManageService courseService;
		string id = this->getLoginUserId().trimmed().toStdString();
		string courseid = ui.allCourseTable->item(row, 0)->text().toStdString();
		if (ui.allCourseTable->item(row, col)->checkState() == Qt::Checked) {
			QMessageBox message(QMessageBox::NoIcon, "确认", "你确定选该课程?", QMessageBox::Yes | QMessageBox::No, NULL);
			if (message.exec() == QMessageBox::Yes)
			{	
				courseService.StuChooseCourse(id,courseid);
				QMessageBox::warning(this, tr("提示"), tr("操作成功!"), QMessageBox::Yes);
			}
			else {
				allCourseTableFlag = 0;
				ui.allCourseTable->item(row, col)->setCheckState(Qt::Unchecked);
				allCourseTableFlag = 1;
				
			}
		}
		else {
			QMessageBox message(QMessageBox::NoIcon, "确认", "你确定退选该课程?", QMessageBox::Yes | QMessageBox::No, NULL);
			if (message.exec() == QMessageBox::Yes)
			{
				courseService.StuReturnCourse(id, courseid);
				QMessageBox::warning(this, tr("提示"), tr("操作成功!"), QMessageBox::Yes);
			}
			else {
				allCourseTableFlag = 0;
				ui.allCourseTable->item(row, col)->setCheckState(Qt::Checked);
				allCourseTableFlag = 1;
			}
		}
	}
}

void Mainwindow::on_saveStudentBtn_clicked() {
	string id = this->getLoginUserId().trimmed().toStdString();

	string name = ui.nameEdit->text().trimmed().toStdString();
	string sex = ui.sexEdit->text().trimmed().toStdString();
	string className = ui.classEdit->text().trimmed().toStdString();
	string deptName = ui.deptNameEdit->text().trimmed().toStdString();
	InfoManageService info;
	Student stu;

	if (name == "" || sex == "" || className == "" || deptName == "")
	{
		QMessageBox::warning(this, tr("Waring"), tr("输入不能为空!"), QMessageBox::Yes);
	}
	else {
		stu.setId(id);
		stu.setName(name);
		stu.setSex(sex);
		stu.setClassName(className);
		stu.setDeptName(deptName);
		info.stuEditInfo(stu);
		QMessageBox::warning(this, tr("Waring"), tr("修改成功!"), QMessageBox::Yes);
	}

}